@isTest
private class TestApplicationTrigger {
      // Methods for testing
static testMethod void CreateEstablishment(){
    test.startTest();
   
	// Perform our data preparation.
	Account com = new Account(name='test',Master_Account_Flag__c  = true);
	insert com;
	RecordType rtEC = [select id,Name from RecordType where sObjectType=:'Application__c' and name =:'Establishment Card'];  
      	
    //Creating the Transaction (Code By Mustafa because of failure in the test Run)....
    Inspection__c ins = new Inspection__c();
    ins.Active__c = 'Yes';
    ins.Company__c = com.id;
    insert ins;

    string recordType15DigitIdEC =rtEC.Id;
    recordType15DigitIdEC = recordType15DigitIdEC.substring(0,recordType15DigitIdEC.length()-3);

    Transaction_Types__c transactionTypeEC = new Transaction_Types__c();
    transactionTypeEC.Name = 'Establishment New';
    transactionTypeEC.Application_Record_Type__c = rtEC.Name;
    transactionTypeEC.Application_Record_Type_ID__c = recordType15DigitIdEC;
    transactionTypeEC.Active__c = true;
    insert transactionTypeEC;

    Application__c testappEC = new Application__c(Inspection_Number__c = ins.id,Transaction_Number__c = 'TestEC ',Transaction_Type__c = transactionTypeEC.Id, RecordType = rtEC,RecordTypeId = rtEC.id,Applicant_First_Name__c='Test',Company__c=com.id);
    
    //fire insert
    insert testappEC;
      	
    //fire update
    testappEC.Immigration_Status__c = 'Completed';
    update testappEC; 
   
      	
}

static testMethod void CreateResidency(){
    test.startTest();
   
	// Perform our data preparation.
	Account com = new Account(name='test',Master_Account_Flag__c  = true);
	insert com;
	RecordType rt = [select id,Name from RecordType where sObjectType=:'Application__c' and name =:'Residency'];  
      	
    //Creating the Transaction (Code By Mustafa because of failure in the test Run)....
    Inspection__c ins = new Inspection__c();
    ins.Active__c = 'Yes';
    ins.Company__c = com.id;
    insert ins;

    string recordType15DigitId =rt.Id;
    recordType15DigitId = recordType15DigitId.substring(0,recordType15DigitId.length()-3);

    Transaction_Types__c transactionType = new Transaction_Types__c();
    transactionType.Name = 'Establishment New';
    transactionType.Application_Record_Type__c = rt.Name;
    transactionType.Application_Record_Type_ID__c = recordType15DigitId;
    transactionType.Active__c = true;
    insert transactionType;

    Application__c testapp = new Application__c(Inspection_Number__c = ins.id,Transaction_Number__c = 'TestEC ',Transaction_Type__c = transactionType.Id, RecordType = rt,RecordTypeId = rt.id,Applicant_First_Name__c='Test',Residency_File_Number__c = '111/111/1111', Unified_ID__c='00001', Residency_Permit_Issue_Date__c = system.today(), Residency_Permit_Expiry_Date__c = system.today(),Company__c=com.id);
    
    //fire insert
    insert testapp;
      	
    //fire update
    testapp.Immigration_Status__c = 'Completed';
    update testapp; 
   
      	
}      
      
    static testMethod void CreateNewContact(){
   
    test.startTest();
   
    // Perform our data preparation.
    Account com = new Account(name='test',Master_Account_Flag__c  = true);
    insert com;
    RecordType rt = [select id,Name from RecordType where sObjectType=:'Application__c' and name =:'Employment'];      
    
    //Creating the Transaction (Code By Mustafa because of failure in the test Run)....
    Inspection__c ins = new Inspection__c();
    ins.Active__c = 'Yes';
    ins.Company__c = com.id;
    insert ins;
    
    string recordType15DigitId =rt.Id;
    recordType15DigitId = recordType15DigitId.substring(0,recordType15DigitId.length()-3);
    
    Transaction_Types__c transactionType = new Transaction_Types__c();
    transactionType.Name = 'Employment';
    transactionType.Application_Record_Type__c = rt.Name;
    transactionType.Application_Record_Type_ID__c = recordType15DigitId;
    transactionType.Active__c = true;
    insert transactionType;

    //**********************************
    System.debug('The Transaction Id => '+transactionType.Id + ' and Record Type Id => '+ transactionType.Application_Record_Type_ID__c);
    
    Application__c testapp = new Application__c(Inspection_Number__c = ins.id,Transaction_Number__c = 'Test ',Transaction_Type__c = transactionType.Id,RecordType = rt,RecordTypeId = rt.id,Applicant_First_Name__c='Test',Applicant_Last_Name__c='test',Company__c=com.id,Passport_Number__c='test',Ultimus_Receipt_Number__c='test', Residency_File_Number__c = '111/111/1111', Unified_ID__c='00001', Entry_Permit_Issue_Date__c = system.today(), Entry_Permit_Expiry_Date__c = system.today());
    
    System.debug('The Record Type Object => '+testapp.RecordType+'The Record Type Id=> ' + testapp.RecordTypeId + ' & The Transaction Type App = > '+ testapp.Transaction_Type__c + ' && The Application First Name => '+ testapp.Applicant_First_Name__c);
    
    //testapp.RecordTypeid =rt[0].id;
    insert testapp;
    
    //Now Update the Application object with the isApproved Flag...
    testapp.isApproved__c = true;
    //fire updates
    testapp.Immigration_Status__c = 'Stop - Company';	
    update testapp;
    
    testapp.Immigration_Status__c = 'Stop - AFZA';	
    update testapp;

    testapp.Immigration_Status__c = 'Normal Rejection';	
    update testapp;

    testapp.Immigration_Status__c = 'Sent to immigration';	
    update testapp;

    testapp.Immigration_Status__c = 'Approved';	
    update testapp;
    
    testapp.Immigration_Status__c = 'Completed';	
    update testapp;

    
     ApexPages.StandardController sc = new ApexPages.StandardController(testapp);
  
      ApexPages.CurrentPage().getParameters().put('id',testapp.id);

       PdfInvoiceController pdfInvoice = new PdfInvoiceController(sc);
       PageReference pg = pdfInvoice.savePdf();
    
	
	ApexPages.currentPage().getParameters().put('id', String.Valueof(ins.id));
    System.debug('The Test App Debug = > '+ testapp.Inspection_Number__c + ' && the Page Id => ' +  ApexPages.currentPage().getParameters().get('id'));

    ApexPages.StandardController mcontroller = new ApexPages.StandardController(ins);
    ext_InspectionReport controller = new ext_InspectionReport(mcontroller); 
    
    // Stop the test, this changes limit context back to test from trigger.
    test.stopTest();
        
    // Query the database for the newly inserted records.
   
}

   }